package com.hjx.face.javacv;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

public class Package {
	public static void main(String[] args) {
		String fileFilter="-linux-;-macosx-;-android-;-windows-x86_64";

		String packageApp = "\"C:\\Program Files (x86)\\Inno Setup 5\\ISCC.exe\"";
		String packageFile = System.getProperty("user.dir")+"\\target\\face";
		delete(packageFile+"\\bin\\jar",fileFilter);
		new Thread(new Runnable() {
			public void run() {
				try {
				String packagePath = packageFile;
				List<String> list = new ArrayList<>();
				list.add("cd " + packageFile);
				list.add(packagePath.substring(0, packagePath.indexOf(":") + 1));
				list.add(packageApp + " " + packageFile+"\\主程序打包.iss");
				list.add("explorer "+packagePath);
				File f = new File(packagePath + "\\package.bat");
					if (!f.exists()) {
						f.createNewFile();
					}
					BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), "GBK"));
					for (String string : list) {
						System.out.println(string);
						bw.write(string);
						bw.newLine();
					}
					bw.close();

					String command = "cmd /c start "+f.getAbsolutePath()+"";
					System.out.println(command);
					Process p = Runtime.getRuntime().exec(command);
					BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream(), "GBK"));
					String readLine = br.readLine();
					if (readLine != null) {
						System.out.println(readLine);
						readLine = br.readLine();
					}
					br.close();
				} catch (Exception e) {
					System.out.println("打包时发生错误"+e);
				}
			}
		}).start();
	}
	protected static void delete(String filePath, String matchs) {
		File file = new File(filePath);
		if (!file.exists()||!file.isDirectory()) {
			throw new RuntimeException("文件夹不存在");
		}
		String[] split = matchs.split(";");
		
		File[] files = file.listFiles();
		for (File f : files) {
			for (String string : split) {
				if (f.getName().contains(string)&&!f.isDirectory()) {
					f.delete();
					System.out.println(f.getAbsolutePath()+":删除完成");
				}
			}
		}
	}
}
